//
//  NSData+FormatString.m
//  CLExtension
//
//  Created by CharlieLiao on 2017/8/4.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "CLFormatString.h"

@interface NSDate(CLDateFormatter)

+ (NSDateFormatter *)cl_DateFormatter;

@end

@implementation NSDate (FormatString)

/// 根据格式返回日期时间字符串
- (NSString*)toString:(NSString*)dateFormat
{
    NSDateFormatter *dateFormatter = [NSDate cl_DateFormatter];
    NSString *reVal = nil;
    @synchronized(dateFormatter) {
        [dateFormatter setDateFormat:dateFormat];
        reVal = [dateFormatter stringFromDate:self];
    }
    return reVal;
}

/// 返回日期时间字符串 如：yyyy-MM-dd HH:mm:ss
-(NSString*)toString
{
    return [self toString:@"yyyy-MM-dd HH:mm:ss"];
}

/// 返回日期时间字符串 如：yyyy-MM-dd HH:mm:ss.SSS
-(NSString*)toMSString
{
    return [self toString:@"yyyy-MM-dd HH:mm:ss.SSS"];
}

/// 返回日期字符串 如：yyyy-MM-dd
- (NSString*)toDateString
{
    return [self toString:@"yyyy-MM-dd"];
}

/// 返回时间字符串 如：HH:mm
- (NSString*)toTimeString
{
    return [self toString:@"HH:mm"];
}

- (NSString*)toDateTimeString
{
    return [self toString:@"yyyy-MM-dd HH:mm"];
}

-(NSString*)toStringWithNoYear
{
    return [self toString:@"MM-dd HH:mm"];
}

///字符串 根据格式yyyy-MM-dd HH:mm:ss 转NSDate
+ (NSDate*)dateWithFormatDate:(NSString*)dateString
{
    return [self dateWithFormatDate:dateString format:@"yyyy-MM-dd HH:mm:ss"];
}

///字符串 根据格式转NSDate
+ (NSDate*)dateWithFormatDate:(NSString*)dateString format:(NSString*)format
{
    NSDateFormatter *dateFormatter = [NSDate cl_DateFormatter];
    NSDate *reVal = nil;
    @synchronized(dateFormatter) {
        [dateFormatter setDateFormat:format];
        reVal = [dateFormatter dateFromString:dateString];
    }
    return reVal;
}


@end

